document.getElementById('calculate').addEventListener('click', function() {
    const headCount = parseInt(document.getElementById('head').value);
    const footCount = parseInt(document.getElementById('foot').value);
    const result = document.getElementById('result');
    let hasSolution = false;

    // 修复循环条件：chicken <= headCount
    for (let chicken = 0; chicken <= headCount; chicken++) {
        const rabbit = headCount - chicken;
        if (chicken * 2 + rabbit * 4 === footCount) {
            result.textContent = `分别有鸡${chicken}只，有兔子${rabbit}只`;
            result.style.color = 'blue';
            hasSolution = true;
            break;
        }
    }

    if (!hasSolution) {
        result.textContent = '无解';
        result.style.color = 'red';
    }
});